define(['jquery','underscore','validation'], function ($,_) {

    return {
        init: function () {
            jQuery.validator.messages = {
                required: "此项必填",
                remote: "请修改",
                email: "请输入正确的Email地址",
                url: "请输入有效的URL",
                date: "请输入有效的日期",
                dateISO: "请输入有效的日期(ISO)",
                number: "请输入有效的数字",
                digits: "请输入整数",
                creditcard: "请输入有效的信用卡号",
                equalTo: "您两次输入的密码不同，请重新输入",
                maxlength: $.validator.format("不能超过 {0} 字符"),
                minlength: $.validator.format("请输入至少 {0} 个字符"),
                rangelength: $.validator.format("输入的内容必须大于 {0} 小于 {1} 个字符"),
                range: $.validator.format("输入的内容必须大于 {0} 小于 {1} 字符"),
                max: $.validator.format("请输入不大于{0}的值"),
                min: $.validator.format("请输入不小于{0}的值")

            };
            jQuery.validator.addClassRules({
                repassword: {
                    password: true,
                    equalTo: '#password'
                },
                plus: {
                    number: true,
                    min: 0
                }
            });

            //密码验证
            //密码必须为6-20位字母或数字
            jQuery.validator.addMethod("password", function (value, element) {
                var tel = /^[\w.]{6,20}$/;
                return this.optional(element) || (tel.test(value));
            }, "密码必须为6-20位字母或数字");

            jQuery.validator.addMethod("english", function (value, element) {
                var tel = /[^\u4e00-\u9fa5]/;
                return this.optional(element) || (tel.test(value));
            }, "此项必填为英文");


            // 邮政编码验证
            jQuery.validator.addMethod("zipcode", function (value, element) {
                var tel = /^[0-9]{6}$/;
                return this.optional(element) || (tel.test(value));
            }, "请正确填写您的邮政编码");

            //电话号码验证
            jQuery.validator.addMethod("phone", function (value, element) {
                var tel = /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/;
                return this.optional(element) || (tel.test(value));
            }, "请正确填写您的电话号码");



            jQuery.validator.addMethod("distinct", function (value, element) {
                var source = value.split('\n'),
                    target = [];
                _.each(source,function(data){
                   if(data!=""){
                       target.push(data);
                   }
                });
                var length = target.length;

                for (var i = 0; i < length; i++) {
                    for (var j = i + 1; j < length; j++) {
                        if (target[i] == target[j]) {
                            return false;
                        }
                    }
                }
                return true;
            }, "输入跟踪号不能重复");

            jQuery.validator.addMethod("1000line", function (value, element) {
                var source = value.split('\n'),
                    target = [];
                _.each(source,function(data){
                    if(data!=""){
                        target.push(data);
                    }
                });
                var length = target.length;

                if (length <= 1000) {
                    return true;
                } else {
                    return false;
                }
            }, "输入跟踪号最多为1000个");


            $('.validform').each(function () {
                var $form = $(this);
                $form.find('input').each(function () {
                    var validateRules = $(this).attr('validate-rules');
                    if (validateRules) {
                        $(this).addClass(validateRules);
                    }
                });
                $form.find('textarea').each(function () {
                    var validateRules = $(this).attr('validate-rules');
                    if (validateRules) {
                        $(this).addClass(validateRules);
                    }
                });
                $form.find('select').each(function () {
                    var validateRules = $(this).attr('validate-rules');
                    if (validateRules) {
                        $(this).addClass(validateRules);
                    }
                });
                $form.validate({
                    errorElement: "span",
                    errorClass: 'error',
                    success: function (error, element) {
                        error.remove();
                    },
                    errorPlacement: function (error, element) {
                        if ($(element).closest('.form-group').length != 0) {
                            error.appendTo($(element).closest('.form-group'));
                        }
                        if ($(element).closest('.edit-column').length != 0) {
                            error.appendTo($(element).closest('.edit-column'));
                        }

                    }
                });
            });

            $('.signform').each(function () {
                var $form = $(this);
                $form.find('input').each(function () {
                    var validateRules = $(this).attr('validate-rules');
                    if (validateRules) {
                        $(this).addClass(validateRules);
                    }
                });
                $form.validate({
                    errorElement: "span",
                    errorClass: 'sign-error',
                    success: function (error, element) {
                        error.remove();
                    }
                });
            });
        }

    }
});
